<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * Record
 * @package	Codeigniter
 * @author	Chhing Chhing
 * @link	abc@gmail.com
 */

class Position extends PNC_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model(array('mod_department'));
    }

    /*
     * Index
     * @access public
     * @return void
     */

    public function index() {
        redirect('position/man_position/');
    }

    public function man_position() {
        if ($this->check_session()) {
            $data['title'] = 'Position Management';
            $this->session->set_userdata("pagination", $this->uri->segment(3));
            $config['total_rows'] = ''; // initialize total department
            $config['base_url'] = site_url("position/man_position/"); // initialize base url

            $config['total_rows'] = $this->mod_department->count_position();
            $config['per_page'] = 10;
            $config['uri_segment'] = 3;
            $choice = $config["total_rows"] / $config["per_page"];
            $config["num_links"] = round($choice);
            $config['first_link'] = 'First';
            $config['next_link'] = 'Next';
            $config['prev_link'] = 'Pre';
            $config['last_link'] = 'Last';
            $this->pagination->initialize($config); //function to show all pages
            $page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;

            $data['position'] = $this->mod_department->m_position($config['per_page'], $page);
            $data['pagination'] = $this->pagination->create_links();
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    public function add_position() {
        $data['title'] = 'Add Position';
        if ($this->input->post('btn_add_pos')) {
            $this->form_validation->set_rules('pos_name', 'Position', 'trim|required|alpha_num_dash_space|is_unique[pnc_position.pos_name]|xss_clean');
            $this->form_validation->set_message('is_unique', 'Position\'s name is exsits in database');
            $this->form_validation->set_message('alpha_num_dash_space', 'The field only contain numeric characters');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_userdata('run_fail', show_message('Can not add new position\'s name!', 'error'));
                $this->view_main($data);
            } else {
                $name = $this->input->post('pos_name');
                $result = $this->mod_department->add_position($name);
                if ($result) {
                    $this->session->set_userdata('add_success', show_message('You have been add position successfully!', 'success'));
                    if ($this->session->userdata('pagination')) {
                        redirect('position/man_position/' . $this->session->userdata('pagination'));
                    }
                    redirect('position/man_position');
                }
                redirect('position/man_position');
            }
        } else {
            $this->view_main($data);
        }
    }

    /* Edit position */

    public function edit_position() {
        if ($this->check_session()) {
            $id = $this->input->post('pos_id');
            $this->session->set_userdata('id', $id);
            $data['title'] = 'Edit Position';
            $data['edit_position'] = $this->mod_department->get_position($this->uri->segment(3));
            if ($this->input->post('btn_edit')) {
//                $this->form_validation->set_rules('txt_pos_name', 'Position Name', 'trim|required|alpha_num_dash_space|is_unique[pnc_position.pos_name]|xss_clean');
                $this->form_validation->set_rules('txt_pos_name', 'Position Name', 'trim|required');

//				$this->form_validation->set_message('is_unique', 'Position\'s name is exsits in database');
//				$this->form_validation->set_message('alpha_num_dash_space', 'The field only contain numeric characters');
                if ($this->form_validation->run() === FALSE) {
                    $this->session->set_userdata('run_fail', show_message('Can not edit position\'s name!', 'error'));
                    redirect('position/edit_position/' . $this->session->userdata('id'));
                } else {
                    $old_pos_name = $this->input->post('old_pos_name');
                    $get_pos_name = !$this->input->post('txt_pos_name') ? $old_pos_name : $this->input->post('txt_pos_name');
                    $result = $this->mod_department->update_position($id, $get_pos_name);
                    if ($result) {
                        $this->session->set_userdata('edit_success', show_message('You have been edit successfully!', 'success'));
                        if ($this->session->userdata('pagination')) {
                            redirect('position/man_position/' . $this->session->userdata('pagination'));
                        }
                        redirect('position/man_position');
                    }
                }
            }

            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    /*
     * Delete Department
     * @access public
     */

    public function delete() {
        $id = $this->uri->segment(3);
        $result = $this->mod_department->deleted($id);
        if ($result) {
            $this->session->set_userdata('del_pos_success', show_message('You have been deleted position successfully!', 'success'));
            if ($this->session->userdata('pagination')) {
                redirect('position/man_position/' . $this->session->userdata('pagination'));
            }
            redirect('position/man_position');
        }
    }

    public function delete_positions() {
        $checked_id = $this->input->post('empCheck');
        /* PNC_Controller::delete_checked($controller, $function, $model, $checked_id, $lea_type, $where, $table) */
        PNC_Controller::delete_checked('position', 'man_position', 'PNC_Model', $checked_id, '', 'pos_id', 'pnc_position');
    }

}
